// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const app =getApp();
Page({
  data: {
    data_login:{
      user:'',
      pw:'',
    },
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  onUserInput: function(e) {
    this.setData({
      'data_login.user': e.detail.value
    });
  },
  onPwInput:function(e){
    this.setData({
      'data_login.pw':e.detail.value
    })
  },
  u_regiest(){
    console.log('跳转注册页面')
    wx.navigateTo({
      url: '/pages/u_register/index',
    })
  },
  d_login(){
    wx.navigateTo({
      url: '/pages/d_login/index',
    })
  },
  a_login(){
    wx.navigateTo({
      url: '/pages/a_login/index',
    })
  },
  u_login(){
    console.log(this.data.data_login)
    wx.request({
      url: 'http://127.0.0.1:8080/api/u_login',
      method:'POST',
      data:this.data.data_login,
      success:(res)=>{
        console.log(res)
        if(res.data.status==200){
          console.log('登陆成功')
          app.globalData.userWhologin=this.data.data_login
          app.globalData.login_type=1;
          console.log(app.globalData.userWhologin)
          wx.reLaunch({
            url: '/pages/indexOfUser/index',
          })
        }else{
          console.log('账号密码填写错误')
          this.setData({
            'data_login.user': '',
            'data_login.pw': ''
          })
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000,
          })
        }

      }
    })
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
